<script type="text/javascript">
var enable_submit = true;
var key = "{{sysconfig_for_ump.name.replace('.', '_')}}";
var obj = {
    'volume_number_interval': 'integer',
    'volume_number_enabled': 'bool',
    'volume_number_threshold': 'over0int-1',
    'data_recover_interval': 'integer',
    'data_recover_qos_limit': 'over0int-1',
    'mail_receive_list': 'email',
    'network_bandwidth_interval': 'integer',
    'network_bandwidth_enabled': 'bool',
    'network_bandwidth_threshold': 'integer',
    'CPU_usage_interval': 'integer',
    'CPU_usage_enabled': 'bool',
    'CPU_usage_threshold': 'usage',
    'oplog_export_path': 'bool',
    'oplog_export_start_date': 'bool',
    'oplog_export_enabled': 'bool',
    'oplog_export_interval': 'integer',
    'memory_usage_interval': 'integer',
    'memory_usage_enabled': 'bool',
    'memory_usage_threshold': 'usage',
    'disk_capacity_interval': 'integer',
    'disk_capacity_enabled': 'bool',
    'disk_capacity_threshold': 'usage',
    'latency_interval': 'integer',
    'latency_enabled': 'bool',
    'latency_threshold': 'integer',
    'volume_delete_enabled': 'bool',
    'volume_delete_period': 'integer'
};
var regs = {'integer':{'reg':/^(\+?[1-9]\d*)$/,'text':'必须是大于0的整数'},
            'usage':{'reg':/^(100)|(^[1-9][0-9]$)|(^[1-9]{1}$)$/,'text':'必须是1到100之间的正整数'},
            'bool': {'reg': null, 'text': null},
            'over0int-1': {'reg': /^(\+?[1-9]\d*)$/, 'text': '必须是大于0的整数'},
            'email': {'reg': /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/, 'text': '请输入合法的邮件地址', 'split': ','}};

function submit_opconfig_update() {
  var sysconfig_value = $("#sysconfig_update_modal").find("#sysconfig_value_cr");
  var bValid = true;
  bValid = bValid && configUpdateCheck(sysconfig_value, regs[obj[key]].reg, regs[obj[key]].text, regs[obj[key]].split);
  if (bValid) {
    if (enable_submit) {
      enable_submit = false;
      var $submitBtn = $('.modal-footer').find('.btn-blue');
      $submitBtn.html('<i class="fa fa-spinner fa-spin" style="margin:3px 6px;"></i>');
      $('.rtn-tip').slideUp('fast');
      var value_setting = sysconfig_value.val() 
      {% if sysconfig_for_ump.name in ['oplog_export.interval'] %}
      value_setting = parseInt(value_setting) * 24 * 3600 {% endif %}
      {% if sysconfig_for_ump.name in ['data_recover.qos_limit'] %}
        if (value_setting != '-1'){
            value_setting = parseInt(value_setting) * 1024 *1024 
        }
        {% endif %}
      $.post("/opconfig_update", {
          "value": value_setting,
          "id": "{{ sysconfig_for_ump.id }}"
        },
        function(data) {
          if (data.reply.is_success) {
            angular.element('#opconfig_controller').scope().close();
            $('#opconfig_table').trigger("reloadGrid")
          } else {
            error = data.reply.error;
            $('.rtn-tip').text(error).slideDown('fast');
            $submitBtn.html(SelfTranslate("[[ 'label ok'|translate ]]"));
            enable_submit = true;
          }
        });
    }
  };
}

function doCalendar($el) {
  $el.calendar({
    setPos: function(targetObj, moveObj) {
      var coords = this.findPos(targetObj);
      moveObj.css({
        'position': 'fixed',
        'left': coords[0] + 'px',
        'top': (coords[1] + targetObj.offsetHeight) + 'px'
      });
    }
  });
}

$(function() {
  if ("{{sysconfig_for_ump.name.replace('.', '_')}}" == "oplog_export_start_date") {
    $("#sysconfig_value_cr").on('focus', function() {
      doCalendar($(this));
    });
  }
  if ("{{sysconfig_for_ump.name.replace('.', '_')}}" == "volume_number_enabled" || "{{sysconfig_for_ump.name.replace('.', '_')}}" == "network_bandwidth_enabled" || "{{sysconfig_for_ump.name.replace('.', '_')}}" == "CPU_usage_enabled" || "{{sysconfig_for_ump.name.replace('.', '_')}}" == "oplog_export_enabled" || "{{sysconfig_for_ump.name.replace('.', '_')}}" == "memory_usage_enabled" || "{{sysconfig_for_ump.name.replace('.', '_')}}" == "disk_capacity_enabled" || "{{sysconfig_for_ump.name.replace('.', '_')}}" == "latency_enabled" || "{{sysconfig_for_ump.name.replace('.', '_')}}" == "volume_delete_enabled") {
    var torf_source = [
      ['False', 'False'],
      ['True', 'True']
    ];
    $('#sysconfig_value_cr').addClass('rounded_dropdown').attr('type','').rqDropDown({
      position: true,
      "datatype": "local",
      "source": torf_source
    });
  }
});
</script>
<div class="modal-header">
  <h3>编辑</h3>
</div>
<div class="modal-body modal-body-height" id="sysconfig_update_modal">
  <div class="opconfig-update-box">
    <div class="rtn-tip"></div>
    <table class="base-info">
      <tr>
        <td>
          <label>模块</label>
        </td>
        <td colspan="3">
          <label id="sysconfig_module" name="sysconfig_module">
            <b>[['{{sysconfig_for_ump.category}}' | translate]]</b>
          </label>
        </td>
      </tr>
      <tr>
        <td>
          <label>名称</label>
        </td>
        <td>
          <label id="sysconfig_key_e" name="sysconfig_key_e">
            <b>[['{{sysconfig_for_ump.name.replace('.', '_')}}' | translate]]</b>
          </label>
        </td>
        <td style="width: 12.5%;text-align: left;">
          <label>值</label><a>*</a>
        </td>
        <td style="width: 37.5%;">
          {% if sysconfig_for_ump.name in ['oplog_export.interval'] %}
          <input id="sysconfig_value_cr" size="25" type="text" value="{{(sysconfig_for_ump.value_setting|int /24/3600)|int}}" /> 
          {% elif sysconfig_for_ump.name in ['data_recover.qos_limit'] and sysconfig_for_ump.value_setting != "-1"%}
          <input id="sysconfig_value_cr" size="25" type="text" value="{{(sysconfig_for_ump.value_setting|int /1024/1024)|int}}" /> 
          {% else %}
          <input id="sysconfig_value_cr" size="25" type="text" value="{{sysconfig_for_ump.value_setting}}" /> {% endif %}
        </td>
      </tr>
    </table>
    <div class="error-tip">
        <p></p>
        <span></span>
    </div>
  </div>
</div>
<div class="modal-footer">
  <button class="btn btn-blue" ng-click="update()">[[ 'label ok'|translate ]]</button>
  <button class="btn btn-white" ng-click="cancel()">[[ 'label cancel'|translate ]]</button>
</div>
